package com.shanjupay.common.utilbak;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class StringUtils {

    public static String StringFilter(String str) throws PatternSyntaxException {
    // 只允许字母和数字 // String regEx ="[^a-zA-Z0-9]";
    // 清除掉所有特殊字符
        String regEx="[`~!#$%^&*+=|{}''\\[\\]<>/?~！#￥%……&*——+|{}【】‘”“’？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String blankFilter(String str){
        if(str == null || "".equals(str)){
            return  null;
        }
        str = str.replaceAll(" ", "");
        str = str.replaceAll(" +","");
        str = str.replaceAll("\\s*", "");
        return  str;
    }

    /**
     *
     * @Title : filterNumber
     * @Type : FilterStr
     * @Description : 过滤出数字
     * @return
     */
    public static String filterNumber(String number)
    {
        number = number.replaceAll("[^(0-9)]", "");
        return number;
    }

    /**
     *
     * @Title : filterAlphabet
     * @Type : FilterStr
     * @Description : 过滤出字母
     * @return
     */
    public static String filterAlphabet(String alph)
    {
        alph = alph.replaceAll("[^(A-Za-z)]", "");
        return alph;
    }

    /**
     *
     * @Title : filterChinese
     * @Type : FilterStr
     * @Description : 过滤出中文
     * @param chin
     * @return
     */
    public static String filterChinese(String chin)
    {
        chin = chin.replaceAll("[^(\\u4e00-\\u9fa5)]", "");
        return chin;
    }

    /**
     *
     * @Title : filter
     * @Type : FilterStr
     * @Description : 过滤出字母、数字和中文
     * @param character
     * @return
     */
    public static String filter(String character)
    {
        character = character.replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5)]", "");
        return character;
    }

    public static boolean isNotBlank(Object o){
        if(o==null){
            return false;
        }
        if(o instanceof List){
            List l = (List)o;
            return l != null;
        }
        if(o instanceof String){
            return isNotBlank((String)o);
        }
        return true;
    }

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        if (null != str && str.trim().length() > 0)
            return true;
        return false;
    }

    public static boolean isNull(Object obj) {
        return obj == null || obj.equals("")
                || obj.toString().trim().equals("null");
    }

    public static String removeEndCharacter(String str){
        if(!isNotBlank(str)){
            return "";
        }
        str = str.substring(0, str.length() - 1);
        return str;
    }

    public static String subStringByByte(String str, int len) {
        String result = null;
        if (str != null) {
            byte[] a = str.getBytes();
            if (a.length <= len) {
                result = str;
            } else if (len > 0) {
                result = new String(a, 0, len);
                int length = result.length();
                if (str.charAt(length - 1) != result.charAt(length - 1)) {
                    if (length < 2) {
                        result = null;
                    } else {
                        result = result.substring(0, length - 1);
                    }
                }
            }
        }
        return result;
    }

    public static String subStringByByteFilterMid(String str, int len) {
        String result = null;
        if (str != null) {
            byte[] a = str.getBytes();
            if (a.length <= len) {
                result = str;
            } else if (len > 0) {
                int n = 0;

                int alenMid = a.length / 2;
                int lenMid = len/2;
                if((lenMid&1)==1){
                    n = 3;
                }else {
                    n = 2;
                }
                int newStringLen = lenMid-n;
                String leftStr = null;
                String rigStr = null;
                leftStr = new String(a,0,newStringLen);
                rigStr = new String(a,alenMid+n,newStringLen);
                result = leftStr + "..."+rigStr;
                //result = new String(a, 0, len);
                /*int length = result.length();
                if (str.charAt(length - 1) != result.charAt(length - 1)) {
                    if (length < 2) {
                        result = null;
                    } else {
                        result = result.substring(0, length - 1);
                    }
                }*/
            }
        }
        return result;
    }

    /**
     * 判断手机号是否合法
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        if (null == phone || "".equals(phone)) {
            return false;
        }
        String regExp = "^1[3,4,5,6,7,8,9]{1}[0-9]{1}[0-9]{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);
        return m.find();
    }

    /**
     * 利用正则表达式判断字符串是否是数字[包括小数点]
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|(0{1}))(\\.\\d{2})$");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 利用正则表达式判断字符串是否是数字[包括小数点]
     * @param str
     * @return
     */
    public static boolean isNumericForTeacher(String str){
        Pattern pattern = Pattern.compile("^[0-9]*$");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 查询参数转义 % & _ 符号
     *
     * @param str
     * @return
     */
    public static String transferCode(String str) {
        str = str.replaceAll("%", "\\\\%");//%25
        str = str.replaceAll("&", "\\\\&");//%26
        str = str.replaceAll("_", "\\\\_");
        str = str.replaceAll("\\\\", "\\\\\\\\");
        return str;
    }

    /**
     * 查询参数转义 % & _ 符号
     *
     * @param str
     * @return
     */
    public static String unTransferCode(String str) {
        str = str.replaceAll("%25", "\\\\%");
        str = str.replaceAll("%26", "\\\\&");
        str = str.replaceAll("_", "\\\\_");
        return str;
    }

    public static void main(String[] args){
        /**
         * 声明字符串you
         */
        String you = "^&^&^you123-$-%$%你好";
        /**
         * 调用过滤出数字的方法
         */
        //you = filterNumber(you);
        /**
         * 打印结果
         */
        //System.out.println("过滤出数字：" + you);

        /**
         * 声明字符串hai
         */
        String hai = "￥%……4556ahihdjsadhj$%$%你好吗wewewe";
        /**
         * 调用过滤出字母的方法
         */
        //hai = filterAlphabet(hai);
        /**
         * 打印结果
         */
        //System.out.println("过滤出字母：" + hai);

        /**
         * 声明字符串dong
         */
        String dong = "$%$%$张三34584yuojk李四@#￥#%%￥……%&";
        /**
         * 调用过滤出中文的方法
         */
        //dong = filterChinese(dong);
        /**
         * 打印结果
         */
        //System.out.println("过滤出中文：" + dong);

        /**
         * 声明字符串str
         */
        String str = "$%$%$张三34584yuojk李四@#￥#%%￥……%&";
        /**
         * 调用过滤出字母、数字和中文的方法
         */
        //str = filter(str);
        /**
         * 打印结果
         */
        //System.out.println("过滤出字母、数字和中文：" + str);
        String s = "绿地英语1701sss11112群";
        int mid = s.getBytes().length / 2;

        /*byte[] ss = s.getBytes();
        for(byte b : ss){
            System.out.println(b);
        }*/

        //String result = subStringByByteFilterMid(s,33);
        //System.out.println("s.length():"+s.length() +"========mid:"+mid+ "====================getBytes().length:"+s.getBytes().length+ "==============subStringByByteFilterMid:"+result+"================result.length:"+result.getBytes().length);

        System.out.println(transferCode("%26"));
    }

}
